package com.wildish.demo.controller;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wildish.core.controller.AbstractController;
import com.wildish.demo.ebo.TestRole;
import com.wildish.demo.ebo.TestUser;
import com.wildish.demo.service.TestUserService;

@Controller()
@RequestMapping("/demo")
public class DemoController extends AbstractController {
	
	@Resource
    private TestUserService userService;  
	
	@RequestMapping
	public ResponseEntity<String> doDemoDefault() {
		return webSupport.getJSONResponseEntity("{\"success\": true, \"msg\": \"hello, world\"}");
	}
	
	@RequestMapping("/user/{userid}")
	public ResponseEntity<String> getTestUser(@PathVariable("userid") Integer userId) {
		TestUser user = userService.getUserById(userId);
		return webSupport.getTextResponseEntity(user.getUserName());
	}
	
	@RequestMapping("/userrole/{userid}")
	public ResponseEntity<String> getTestUserWithRole(@PathVariable("userid") Integer userId) {
		TestUser user = userService.getUserRoleById(userId);
		
		String result = user.getUserName();
		result += ", role: ";
		for(TestRole role : user.getRoleList()) {
			result += role.getRoleName() + " ";
		}
		return webSupport.getTextResponseEntity(result);
	}
}
